﻿using System.IO;
using System.Text;
using System;
using System.Collections.Generic;
using UtilLib;

namespace ProtocolTool
{
    partial class Program
    {
        public static readonly string Filepath_Html = @"协议大纲.html";
        public static readonly string Template_Html = @"<!DOCTYPE html>
<html>
<head>
    <title>协议大纲</title>
    <meta charset=""utf-8"">
    <meta name=""viewport"" content=""width=device-width, initial-scale=1"">
    <link rel=""stylesheet"" href=""https://cdn.staticfile.org/twitter-bootstrap/4.3.1/css/bootstrap.min.css"">
    <script src=""https://cdn.staticfile.org/jquery/3.2.1/jquery.min.js""></script>
    <script src=""https://cdn.staticfile.org/popper.js/1.15.0/umd/popper.min.js""></script>
    <script src=""https://cdn.staticfile.org/twitter-bootstrap/4.3.1/js/bootstrap.min.js""></script>
</head>
<body>

<div class=""container"">

$CONTENT$

</div>

</body>
</html>

";

        /// <summary>
        /// 生成 html 文件
        /// </summary>
        private static void ProtocolConverterHtml()
        {
            Show("ProtocolConverterHtml");
            var sb = new StringBuilder();

            Dictionary<string, Dictionary<string, Dictionary<string, CClass>>> DictDictDictClass = new Dictionary<string, Dictionary<string, Dictionary<string, CClass>>>();
            string md1 = "";
            string md2 = "";
            foreach (var nn1 in DictClass.Values)
            {
                if (nn1.Mode1 != md1)
                {
                    md1 = nn1.Mode1;
                }
                if (!DictDictDictClass.TryGetValue(md1, out var d1))
                {
                    d1 = new Dictionary<string, Dictionary<string, CClass>>();
                    DictDictDictClass[md1] = d1;
                }
                if (nn1.Mode2 != md2)
                {
                    md2 = nn1.Mode2;
                }
                if (!d1.TryGetValue(md2, out var d2))
                {
                    d2 = new Dictionary<string, CClass>();
                    d1[md2] = d2;
                }
                d2[nn1.Name] = nn1;
            }
            foreach (var kvp1 in DictDictDictClass)
            {
                string id1 = RandomString();
                sb.AppendLine($"<div>");
                sb.AppendLine($"<a href=\"#{id1}\" class=\"btn btn-primary btn-block\" data-toggle=\"collapse\">{kvp1.Key}</a>");
                sb.AppendLine($"<div id=\"{id1}\" class=\"collapse\">");
                foreach (var kvp2 in kvp1.Value)
                {
                    string id2 = RandomString();
                    sb.AppendLine($"<div>");
                    sb.AppendLine($"<a href=\"#{id2}\" class=\"btn btn-info btn-block\" data-toggle=\"collapse\">{kvp2.Key}</a>");
                    sb.AppendLine($"<div id=\"{id2}\" class=\"collapse\">");
                    string id0 = RandomString();
                    sb.AppendLine($"<div id=\"{id0}\">");
                    int idx0 = 0;
                    foreach (var item in kvp2.Value.Values)
                    {
                        string id3 = RandomString();
                        string title = true switch
                        {
                            _ when item.ClassType == 0 => $"协议类 {item.Name} {item.Desc}",
                            _ => $"公共类 {item.Name} {item.Desc}"
                        };
                        sb.AppendLine($"<div class=\"card\">");
                        sb.AppendLine($"<div class=\"card-header\">");
                        if (idx0 == 0)
                        {
                            sb.AppendLine($"<a class=\"card-link\" data-toggle=\"collapse\" href=\"#{id3}\">{title}</a>");
                            sb.AppendLine($"</div>");
                            sb.AppendLine($"<div id=\"{id3}\" class=\"collapse show\" data-parent=\"#{id0}\">");
                        }
                        else
                        {
                            sb.AppendLine($"<a class=\"collapsed card-link\" data-toggle=\"collapse\" href=\"#{id3}\">{title}</a>");
                            sb.AppendLine($"</div>");
                            sb.AppendLine($"<div id=\"{id3}\" class=\"collapse\" data-parent=\"#{id0}\">");
                        }
                        sb.AppendLine($"<div class=\"card-body\">");
                        sb.AppendLine($"<p>----- {item.Name} ----- {item.NameId} ----- {item.Id} -----</p>");
                        foreach (var bb2 in item.DictBody.Values)
                        {
                            sb.AppendLine($"<p>{GetClassBodyHtml(bb2)}</p>");
                        }
                        sb.AppendLine($"</div>");
                        sb.AppendLine($"</div>");
                        sb.AppendLine($"</div>");
                        idx0++;

                        //sb.AppendLine($"<div>");
                        //sb.AppendLine($"<a href=\"#{id3}\" class=\"btn btn-success btn-block\" data-toggle=\"collapse\">{title}</a>");
                        //sb.AppendLine($"<div id=\"{id3}\" class=\"collapse show\">");
                        //sb.AppendLine($"<p>----- {item.Name} ----- {item.NameId} ----- {item.Id} -----</p>");
                        //foreach (var bb2 in item.DictBody.Values)
                        //{
                        //    sb.AppendLine($"<p>{GetClassBodyHtml(bb2)}</p>");
                        //}
                        //sb.AppendLine($"</div>");
                        //sb.AppendLine($"</div>");
                    }
                    sb.AppendLine($"</div>");
                    sb.AppendLine($"</div>");
                    sb.AppendLine($"</div>");
                }
                sb.AppendLine($"</div>");
                sb.AppendLine($"</div>");
            }
            string txt = Template_Html;
            FileStream fs = new FileStream(PathCurrent + Filepath_Html, FileMode.Create);
            StreamWriter sw = new StreamWriter(fs, new System.Text.UTF8Encoding(false));
            txt = txt.Replace("$CONTENT$", sb.ToString());
            sw.Write(txt);
            sw.Close();
            Show("输出文件：" + Filepath_Html);
        }

        //成员和初始化
        public static string GetClassBodyHtml(CClassNode v)
        {
            var sb = new StringBuilder();
            switch (v.Ctype)
            {
                case "list":
                    sb.Append($"List[{v.Ctype1}] {v.Body}");
                    break;
                case "dict":
                    sb.Append($"Dictionary[{v.Ctype1}, {v.Ctype2}] {v.Body}");
                    break;
                default:
                    sb.Append($"{v.Ctype} {v.Body}");
                    break;
            }
            if (!string.IsNullOrEmpty(v.Desc))
            {
                sb.Append($"  //{v.Desc}");
            }
            return sb.ToString();
        }

        private static char[] 随机字符小写 = {
            'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'
        };
        private static char[] 随机字符数字 = {
            '0','1','2','3','4','5','6','7','8','9'
        };
        private static string RandomString()
        {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < 5; i++)
            {
                sb.Append(随机字符小写.RandomSingle());
            }
            for (int i = 0; i < 5; i++)
            {
                sb.Append(随机字符数字.RandomSingle());
            }
            return sb.ToString();
        }

    }
}